﻿using System;
using System.Web.UI;
using MS.InformationManagement.Presenter.SimpleSearch;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Controls;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.WP.DocumentCenter.SimpleSearch
{
    public partial class SimpleSearchUserControl : BaseControl, ISimpleSearchView
    {
        private int _TotalExecutionTime;

        #region [Private Fields]
        SimpleSearchPresenter _presenter = null;
        #endregion

        #region [Event Handlers]

        protected void ddlResultsPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageIndex = 0;
                PageSize = Convert.ToInt32(ddlResultsPerPage.SelectedValue);
                _presenter.ExecuteSimpleSearch(false);
                Pager1.BuildPager();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string searchKeyWord = Request.QueryString.Get("k");
                if (!string.IsNullOrEmpty(searchKeyWord))
                {
                    try
                    {
                        SearchCriteria = searchKeyWord;
                        _presenter.ExecuteSimpleSearch(true);
                        Pager1.BuildPager();
                    }
                    catch (Exception ex)
                    {
                        HandleError(ex);
                    }
                }
            }

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            _presenter = new SimpleSearchPresenter(this);            
            grdViewSearchResults.RowDataBound += new System.Web.UI.WebControls.GridViewRowEventHandler(grdViewSearchResults_RowDataBound);
            Pager1.OnPageChanged += new InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls.Pager.PageChangeHandler(OnPageChanged);
            //Pager1.NextLinkText = "Siguiente";
            //Pager1.PreviousLinkText = "Anterior";
            //Pager1.NumberOfPagesToDisplay = 10;
            //PageSize = 5;
            //PageIndex = 0;
            btnImgSearch.AddLoader();
        }

        protected void OnPageChanged(object sender, PageChangeEventArgs e)
        {
            PageSize = Convert.ToInt32(ddlResultsPerPage.SelectedValue);
            _presenter.ExecuteSimpleSearch(false);
            Pager1.BuildPager();
        }

        protected void grdViewSearchResults_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            //if (e.Row.DataItem != null)
            //{
            //    MS.InformationManagement.DAL.DTOs.RadicadoSearchDTO currentItem = e.Row.DataItem as MS.InformationManagement.DAL.DTOs.RadicadoSearchDTO;
            //    e.Row.Cells[8].Text = !string.IsNullOrEmpty(currentItem.CompanyName) ? string.Format("{0} {1}", currentItem.CompanyName, currentItem.CompanyLegalRepresentative)
            //        : !string.IsNullOrEmpty(currentItem.FirstName) ? string.Format("{0} {1} {2}", currentItem.FirstName, currentItem.LastName1, currentItem.LastName2)
            //        : "No Aplica";
            //}
        }
        
        protected void btnImgSearch_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                PageIndex = 0;
                _presenter.ExecuteSimpleSearch(true);
                Pager1.BuildPager();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }
        #endregion

        #region [ISimpleSearchView Members]
        public System.Collections.Generic.IEnumerable<DAL.DTOs.RadicadoSearchDTO> CurrentPageResultSet
        {
            get
            {
                return grdViewSearchResults.DataSource as System.Collections.Generic.IEnumerable<DAL.DTOs.RadicadoSearchDTO>;
            }
            set
            {
                grdViewSearchResults.DataSource = value;
                grdViewSearchResults.DataBind();
            }
        }

        public string SearchCriteria
        {
            get
            {
                return txtbSearchBox.Text;
            }
            set
            {
                txtbSearchBox.Text = value;
            }
        }

        public int TotalExecutionTime
        {
            get { return _TotalExecutionTime; }
            set
            {
                _TotalExecutionTime = value;
                ltrQueryTime.Text = string.Format("La consulta tardo {0:0.####} segundo(s)", Convert.ToDouble(value) / Convert.ToDouble(1000));
                ltrQueryTime.Visible = true;
            }
        }

        public int PageIndex
        {
            get { return Pager1.PageIndex; }

            set { Pager1.PageIndex = value; }
        }

        public int PageSize
        {
            get { return Pager1.PageSize; }
            set { Pager1.PageSize = value; }
        }

        public int TotalRecords
        {
            get { return Pager1.TotalRecords; }
            set
            {
                Pager1.TotalRecords = value;
                ltrTotalRecords.Text = string.Format(", {0} resultado(s) encontrados.", value);
                ltrTotalRecords.Visible = value > 0;
            }
        }
        #endregion

        #region [Public Members]

        public void ExecuteSearchFromHistory(DAL.DocumentCenter.SearchConfig config) {
            try
            {
                SearchCriteria = config.SimpleSearchCriteria;
                PageIndex = 0;
                _presenter.ExecuteSimpleSearch(false);
                Pager1.BuildPager();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        protected void lnkVer_DataBinding(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.HyperLink trigger = sender as System.Web.UI.WebControls.HyperLink;
            trigger.NavigateUrl = string.Format("javascript:openDialogAutoSize('{0}');", trigger.NavigateUrl);
        }

    }
}
